home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADVANCED / TVERTEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.8 KB  |  282 lines

  1.  
  2. /* tvertex.c - by David Blythe (with help from Mark Kilgard), SGI */
  3.  
  4. /* T-vertex artifacts example.  The moral: Avoid vertex edge junctions that
  5.    make a T-shape. */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <GL/glut.h>
  10. #include <math.h>
  11.  
  12. static float scale = 1.;
  13. static float transx = 0, transy = 0;
  14. static float rotx = 29, roty = -21;  /* Initially askew. */
  15. static int ox = -1, oy = -1;
  16. static int show_t = 1;
  17. static int mot;
  18. #define PAN    1
  19. #define ROT    2
  20.  
  21. void
  22. pan(int x, int y)
  23. {
  24.   transx += (x - ox) / 500.;
  25.   transy -= (y - oy) / 500.;
  26.   ox = x;
  27.   oy = y;
  28.   glutPostRedisplay();
  29. }
  30.  
  31. void
  32. rotate(int x, int y)
  33. {
  34.   rotx += x - ox;
  35.   if (rotx > 360.)
  36.     rotx -= 360.;
  37.   else if (rotx < -360.)
  38.     rotx += 360.;
  39.   roty += y - oy;
  40.   if (roty > 360.)
  41.     roty -= 360.;
  42.   else if (roty < -360.)
  43.     roty += 360.;
  44.   ox = x;
  45.   oy = y;
  46.   glutPostRedisplay();
  47. }
  48.  
  49. void
  50. motion(int x, int y)
  51. {
  52.   if (mot == PAN)
  53.     pan(x, y);
  54.   else if (mot == ROT)
  55.     rotate(x, y);
  56. }
  57.  
  58. void
  59. mouse(int button, int state, int x, int y)
  60. {
  61.   if (state == GLUT_DOWN) {
  62.     switch (button) {
  63.     case GLUT_LEFT_BUTTON:
  64.       mot = PAN;
  65.       motion(ox = x, oy = y);
  66.       break;
  67.     case GLUT_MIDDLE_BUTTON:
  68.       mot = ROT;
  69.       motion(ox = x, oy = y);
  70.       break;
  71.     }
  72.   } else if (state == GLUT_UP) {
  73.     mot = 0;
  74.   }
  75. }
  76.  
  77. void
  78. toggle_t(void)
  79. {
  80.   show_t ^= 1;
  81. }
  82.  
  83. void
  84. wire(void)
  85. {
  86.   static int w;
  87.  
  88.   if (w ^= 1)
  89.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  90.   else
  91.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  92. }
  93.  
  94. void
  95. light(void)
  96. {
  97.   static int l = 1;
  98.  
  99.   if (l ^= 1)
  100.     glEnable(GL_LIGHTING);
  101.   else
  102.     glDisable(GL_LIGHTING);
  103. }
  104.  
  105. void
  106. up(void)
  107. {
  108.   scale += .1;
  109. }
  110.  
  111. void
  112. down(void)
  113. {
  114.   scale -= .1;
  115. }
  116.  
  117. void
  118. help(void)
  119. {
  120.   printf("Usage: tvertex\n");
  121.   printf("'h'            - help\n");
  122.   printf("'l'            - toggle lighting\n");
  123.   printf("'t'            - toggle T vertex\n");
  124.   printf("'w'            - toggle wireframe\n");
  125.   printf("'UP'           - scale up\n");
  126.   printf("'DOWN'         - scale down\n");
  127.   printf("left mouse     - pan\n");
  128.   printf("middle mouse   - rotate\n");
  129. }
  130.  
  131. void
  132. init(void)
  133. {
  134.   GLfloat pos[4] =
  135.   {0.0, 0.0, 1.0, 1.0};
  136.  
  137.   glMatrixMode(GL_PROJECTION);
  138.   glLoadIdentity();
  139.   gluPerspective(50., 1., .1, 10.);
  140.   glMatrixMode(GL_MODELVIEW);
  141.   glLoadIdentity();
  142.   glTranslatef(0., 0., -3.5);
  143.  
  144.   /* The default light is "infinite"; a local light is important to ensure
  145.      varying lighting color calculations at the vertices. */
  146.   glLightfv(GL_LIGHT0, GL_POSITION, pos);
  147.  
  148.   glEnable(GL_LIGHT0);
  149.   glEnable(GL_LIGHTING);
  150. }
  151.  
  152. void
  153. display(void)
  154. {
  155.   glClear(GL_COLOR_BUFFER_BIT);
  156.   glPushMatrix();
  157.   glTranslatef(transx, transy, 0.f);
  158.   glRotatef(rotx, 0., 1., 0.);
  159.   glRotatef(roty, 1., 0., 0.);
  160.   glScalef(scale, scale, 1.);
  161.   if (show_t) {
  162.     glBegin(GL_QUADS);
  163.     glVertex2f(-1., 0.);
  164.     glVertex2f(0., 0.);
  165.     glVertex2f(0., 1.);
  166.     glVertex2f(-1., 1.);
  167.  
  168.     glVertex2f(0., 0.);
  169.     glVertex2f(1., 0.);
  170.     glVertex2f(1., 1.);
  171.     glVertex2f(0., 1.);
  172.  
  173.     glVertex2f(-1., -1.);
  174.     glVertex2f(1., -1.);
  175.     glVertex2f(1., 0.);
  176.     glVertex2f(-1., 0.);
  177.     glEnd();
  178.   } else {
  179.     glBegin(GL_QUADS);
  180.     glVertex2f(-1., 0.);
  181.     glVertex2f(0., 0.);
  182.     glVertex2f(0., 1.);
  183.     glVertex2f(-1., 1.);
  184.  
  185.     glVertex2f(0., 0.);
  186.     glVertex2f(1., 0.);
  187.     glVertex2f(1., 1.);
  188.     glVertex2f(0., 1.);
  189.  
  190.     glVertex2f(-1., -1.);
  191.     glVertex2f(0., -1.);
  192.     glVertex2f(0., 0.);
  193.     glVertex2f(-1., 0.);
  194.  
  195.     glVertex2f(0., -1.);
  196.     glVertex2f(1., -1.);
  197.     glVertex2f(1., 0.);
  198.     glVertex2f(0., 0.);
  199.     glEnd();
  200.   }
  201.   glPopMatrix();
  202.   glutSwapBuffers();
  203. }
  204.  
  205. void
  206. reshape(int w, int h)
  207. {
  208.   glViewport(0, 0, w, h);
  209. }
  210.  
  211. /* ARGSUSED1 */
  212. void
  213. key(unsigned char key, int x, int y)
  214. {
  215.   switch (key) {
  216.   case 'l':
  217.     light();
  218.     break;
  219.   case 't':
  220.     toggle_t();
  221.     break;
  222.   case 'w':
  223.     wire();
  224.     break;
  225.   case 'h':
  226.     help();
  227.     break;
  228.   case '\033':
  229.     exit(0);
  230.     break;
  231.   default:
  232.     return;
  233.   }
  234.   glutPostRedisplay();
  235. }
  236.  
  237. /* ARGSUSED1 */
  238. void
  239. special(int key, int x, int y)
  240. {
  241.   switch (key) {
  242.   case GLUT_KEY_UP:
  243.     up();
  244.     break;
  245.   case GLUT_KEY_DOWN:
  246.     down();
  247.     break;
  248.   default:
  249.     return;
  250.   }
  251.   glutPostRedisplay();
  252. }
  253.  
  254. void
  255. menu(int value)
  256. {
  257.   key((unsigned char) value, 0, 0);
  258. }
  259.  
  260. int
  261. main(int argc, char **argv)
  262. {
  263.   glutInit(&argc, argv);
  264.   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  265.   (void) glutCreateWindow("T vertex artifact demo");
  266.   init();
  267.   glutDisplayFunc(display);
  268.   glutKeyboardFunc(key);
  269.   glutSpecialFunc(special);
  270.   glutReshapeFunc(reshape);
  271.   glutMouseFunc(mouse);
  272.   glutMotionFunc(motion);
  273.   glutCreateMenu(menu);
  274.   glutAddMenuEntry("Toggle T vertex", 't');
  275.   glutAddMenuEntry("Toggle wireframe/solid", 'w');
  276.   glutAddMenuEntry("Toggle lighting", 'l');
  277.   glutAddMenuEntry("Quit", '\033');
  278.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  279.   glutMainLoop();
  280.   return 0;             /* ANSI C requires main to return int. */
  281. }
  282.